﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;

using SandcastleBuilder.Utils;

namespace SandcastleBuilder.PlugIns
{
    public partial class Docx_BuilderConfigDlg : Form
    {
        private XmlDocument config;     // The configuration

        /// <summary>
        /// This is used to return the configuration information
        /// </summary>
        public string Configuration
        {
            get { return config.OuterXml; }
        }

        public Docx_BuilderConfigDlg(string currentConfig)
        {
            XPathNavigator navigator, root;

            InitializeComponent();

            // Load the current settings
            config = new XmlDocument();
            config.LoadXml(currentConfig);
            navigator = config.CreateNavigator();

            root = navigator.SelectSingleNode("configuration");

            if (root.IsEmptyElement)
                return;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            XmlNode root, node;

            root = config.SelectSingleNode("configuration");

            node = root.SelectSingleNode("elementsDoc");
            if (node == null)
            {
                node = config.CreateNode(XmlNodeType.Element,
                    "elementsDoc", null);
                root.AppendChild(node);

                XmlAttribute attrContent = config.CreateAttribute("content");
                node.Attributes.Append(attrContent);
            }

            if (chkBoxContent.Checked)
                node.Attributes["content"].Value = "true";
            else
                node.Attributes["content"].Value = "false";

            node = root.SelectSingleNode("convert");
            if (node == null)
            {
                node = config.CreateNode(XmlNodeType.Element,
                    "convert", null);
                root.AppendChild(node);

                XmlAttribute attrPDF = config.CreateAttribute("PDF");
                node.Attributes.Append(attrPDF);
                XmlAttribute attrXPS = config.CreateAttribute("XPS");
                node.Attributes.Append(attrXPS);
            }

            if (convertListBox.GetItemChecked(0))
                node.Attributes["PDF"].Value = "true";
            else
                node.Attributes["PDF"].Value = "false";

            if (convertListBox.GetItemChecked(1))
                node.Attributes["XPS"].Value = "true";
            else
                node.Attributes["XPS"].Value = "false";

            node = root.SelectSingleNode("style");
            if (node == null)
            {
                node = config.CreateNode(XmlNodeType.Element,
                    "style", null);
                root.AppendChild(node);

                XmlAttribute styleName = config.CreateAttribute("name");
                node.Attributes.Append(styleName);
            }

            if(comboBoxStyle.Text.Length == 0)
                node.Attributes["name"].Value = "simple";
            else
                node.Attributes["name"].Value = comboBoxStyle.Text;

            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
